package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.manage.config.Config;
import com.manage.model.SpCodeBanTime;
//import com.manage.model.vo.VSpCodeBanTime;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class SpCodeBanTimeServiceImpl extends AbstractService implements SpCodeBanTimeService{
	private Logger logger;
	public SpCodeBanTimeServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	@Override
	public SpCodeBanTime getSpCodeBanTimeById(int id) {
		String sql = "select * from spcodebantime where id="+id;
		ModelGenerator<SpCodeBanTime> dbcpmg = new ModelGenerator<SpCodeBanTime>(SpCodeBanTime.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<SpCodeBanTime> getSpCodeBanTimesBySpCodeId(int spCodeId) {
		String sql = "select * from spcodebantime where spcodeid="+spCodeId+" order by createdate desc";
		ModelGenerator<SpCodeBanTime> dbcpmg = new ModelGenerator<SpCodeBanTime>(SpCodeBanTime.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean saveSpCodeBanTime(SpCodeBanTime spCodeBanTime, String type) {
		String sql ="";
		if(type.equalsIgnoreCase("insert"))
		{
			spCodeBanTime.setId(getTableId("spcodebantime"));
			sql = "insert into spcodebantime(id,spcodeid,bantimeprovince,bantimestart,bantimeend" +
					",createuser,createdate) values (" +
					""+spCodeBanTime.getId()+",'"+spCodeBanTime.getSpCodeId()+
					"','"+spCodeBanTime.getBanTimeProvince()+"','"+spCodeBanTime.getBanTimeStart()+
					"','"+spCodeBanTime.getBanTimeEnd()+
					"','"+spCodeBanTime.getCreateUser()+
					"','"+spCodeBanTime.getCreateDate()+"')";
		}else if (type.equalsIgnoreCase("update"))
		{
			sql="update spcodebantime set bantimestart='"+spCodeBanTime.getBanTimeStart()+
				"',bantimeend='"+spCodeBanTime.getBanTimeEnd()+
				"',bantimeprovince='"+spCodeBanTime.getBanTimeProvince()+"' where " +
					"id ="+spCodeBanTime.getId();
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql="delete from spcodebantime where " +
			"id ="+spCodeBanTime.getId();
		}
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
//	@Override
//	public List<VSpCodeBanTime> getSpCodeBanTimes() {
//		String sql = "select * from spcodebantime order by createdate desc";
//		ModelGenerator<VSpCodeBanTime> dbcpmg = new ModelGenerator<VSpCodeBanTime>(VSpCodeBanTime.class);
//		return dbcpmg.generateList(dbcp, sql);
//	}

}
